package com.wimcorp.magic.client.input;

import java.util.concurrent.Callable;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.fusesource.jansi.Ansi.Color;

import com.wimcorp.magic.client.CommandLineInterface;

public class ConjureManaSelector extends Selector implements Callable<Void> {

	private String message;
	
	public ConjureManaSelector(CommandLineInterface commandLineInterface,String message) {
		super(commandLineInterface);
		this.message=message;
	}

	@Override
	public Void call() {
		select();
		return null;
	}

	private void select() {
		Options options = getSelectOptions();
		ConsoleInput selectConsole = new ConsoleInput();
		ConsoleInput mainConsole = getCommandLineInterface().getMainConsole();
		CommandLineParser parser = new DefaultParser();
		setRun(true);
		while (isRun()) {
			mainConsole.cancel();
			getCommandLineInterface().printInstruction(message);
			try {
				CommandLine line = parser.parse(options, selectConsole.readLine().split(" "));
				processCommands(options, line.getOptions());
			} catch (Exception e) {
				getCommandLineInterface().print(e.getMessage(), Color.RED);
			}
		}
		synchronized (mainConsole.getLock()) {
			mainConsole.getLock().notifyAll();
		}
	}

	

	public Options getSelectOptions() {
		Options options = new Options();
		options.addOption(getHelpOption());
		options.addOption(getDoneOption());
		options.addOption(getTapLandOption());
		options.addOption(getUnTapLandOption());
		options.addOption(getPlayerListOption());
		options.addOption(getViewBattleFieldOption());
		options.addOption(getViewGraveyardOption());
		options.addOption(getViewHandOption());
		options.addOption(getViewLifeOption());
		return options;
	}

}
